package model;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import utilities.CRes;
import utilities.GameResource;
import font.BitmapFont;

public class AnimalInfo extends ItemInfo {
	public short m_sFood;
	public short m_sHealth;
	public short m_sTimeLiving; //thoi gian song cua 1 con vat: ngay
	public short m_sTimeCreateMoney;//thoi gian cho tien cua vat nuoi khi o giai doan truong thanh: phut
	public short m_sTimeFeed;//thoi gian cho vat nuoi an: gio
	public short m_sTimeTakeABath;//thoi gian tam cho vat nuoi: gio
	public short m_sMoneyHarvest;//so tien ban duoc khi truong thanh
	public short m_sMoneyPeriod;//gia tri cua 1 dong tien sinh ra

	public Part[] m_headPart;
	public Part[] m_bodyPart;	
	
	public String m_strSmallHead;
	public String m_strSmallBody;
	public String m_strHead;
	public String m_strBody;
	public String m_strProperties;
	
	//properties
	public short[] m_iW;
	public short[] m_iH;
	
	public int m_iTimeChangeAction;//thoi gian thay doi hoat dong
	public float[] m_fSpeed;//toc do
	public byte[] m_iTimeChangeFrame;//thoi gian chuyen frame
	public byte[] m_arrRunFrame0;
	public byte[] m_arrRunFrame1;
	
	//...
	
	public AnimalInfo(){
		m_headPart = new Part[2];
		m_bodyPart = new Part[2];
		for(int i = 0; i < 2; i++){
			m_headPart[i] = new Part();
			m_bodyPart[i] = new Part();
		}
		
		m_iW = new short[2];
		m_iH = new short[2];
		m_fSpeed = new float[2];
		m_iTimeChangeFrame = new byte[2];
		
		
	}

	//init animal info
	public void initDescription(){
		super.initDescription();
		m_vtDescription.addElement(m_strName);
		String str = "";
		if(m_iMoney[0] > 0)
			str += GameResource.gold + GameResource.space + m_iMoney[0];
		if(m_iMoney[1] > 0)
			str += GameResource.space + GameResource.dina + GameResource.space + m_iMoney[1];
		
		m_vtDescription.addElement(str);
		
		///////////////////
		str = "Kinh nghiệm: " + m_sExperience + " Cấp độ: " + m_sLevelRequired;
		m_vtDescription.addElement(str);
		
		str = "Thời gian sống " + m_sTimeLiving +" ngày";
		m_vtDescription.addElement(str);
		
		str = "Thời gian ăn " + m_sTimeFeed + " giờ/lần";
		m_vtDescription.addElement(str);
		
		str = "Thời gian tắm " + m_sTimeTakeABath + " giờ/lần";
		m_vtDescription.addElement(str);
		
		str = "Tổng tiền thu hoạch " + m_sMoneyHarvest +" Gold";
		m_vtDescription.addElement(str);
		
		int max = -999;
		int a = m_vtDescription.size();
		for(int i = 0; i < a; i++){
			str = (String)m_vtDescription.elementAt(i);
			int length = BitmapFont.m_facedeFont.stringWidth(str);
			if(length > max)
				max = length;
		}	
		
		m_iWidthInfo = max + 20;
		m_iHeightInfo = 115;
	}

	public void paintInRow(Graphics g, int x, int y, int width, int height, boolean isFocus) {
		// TODO Auto-generated method stub
	}
	
	//paint in shop
	public void paintIcon(Graphics g, int x, int y){
		int tmpX = Math.abs(m_headPart[1].m_iW - 32);
		int tmpY = Math.abs(m_headPart[1].m_iH - 32);
		
		m_headPart[1].paintPart(g, curHeadFr, x + tmpX / 2, y + tmpY / 2, 0);
		
		//nham mo mat cac con thu
		EyesTick();
	}
	
	private void EyesTick(){
		//nham mo mat cac con thu
		tickHead--;
		if(tickHead < 0){
			curHeadFr = 0;//nham mo
			tickHead = CRes.random(20, 30);
		}
		if(curHeadFr == 0 && tickHead < 3){
			curHeadFr = 1;//mo nham
		}
	}
	
	private int curHeadFr = 0;
	private int tickHead = CRes.random(20, 30);
	private int tickChangeFr = 0;
	private int curBodyFr = 0;
	
	
	public void paintAction(Graphics g, int animalStatus, int action, int x, int y, int direction){
		int trans = ((direction == Direction.RIGHT)? 0: Sprite.TRANS_MIRROR);
		int frame;
		
		switch(action){
		case Animal.RUN:
		case Animal.TAKEABATH:
		case Animal.EAT:
			if(animalStatus == 0)
				frame = m_arrRunFrame0[curBodyFr];
			else
				frame = m_arrRunFrame1[curBodyFr];
						
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], m_bodyPart[animalStatus].getX(frame, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			m_headPart[animalStatus].paintPart(g, curHeadFr, frame, x, y, trans);
			m_bodyPart[animalStatus].paintPart(g, frame, frame, x, y, trans);
			
			//update for run
			tickChangeFr++;
			if(tickChangeFr > m_iTimeChangeFrame[animalStatus]){
				if(animalStatus == 0){
					curBodyFr = (curBodyFr + 1) % m_arrRunFrame0.length;
				}
				else{
					curBodyFr = (curBodyFr + 1) % m_arrRunFrame1.length;				
				}
				tickChangeFr = 0;
			}
			break;
			
		case Animal.SITTING:
			if(m_bodyPart[animalStatus].m_nFrame == 5)
				frame = m_bodyPart[animalStatus].m_nFrame - 2;
			else
				frame = m_bodyPart[animalStatus].m_nFrame - 1;
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], m_bodyPart[animalStatus].getX(frame, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			m_headPart[animalStatus].paintPart(g, curHeadFr, frame, x, y, trans);
			m_bodyPart[animalStatus].paintPart(g, frame, frame, x, y, trans);
			break;
			
		case Animal.STAND:
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], m_bodyPart[animalStatus].getX(0, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			m_headPart[animalStatus].paintPart(g, curHeadFr, 0, x, y, trans);
			m_bodyPart[animalStatus].paintPart(g, 0, 0, x, y, trans);
			
			break;
		case Animal.SLEEP://luon luon lay frame cuoi de ve
			frame = m_bodyPart[animalStatus].m_nFrame - 1;
			g.drawImage(GameResource.instance.m_arrShadow[animalStatus], m_bodyPart[animalStatus].getX(frame, x, trans), y - GameResource.instance.m_arrShadow[animalStatus].getHeight()/2, 0);
			m_headPart[animalStatus].paintPart(g, 1, frame, x, y, trans);
			m_bodyPart[animalStatus].paintPart(g, frame, frame, x, y, trans);
			
			break;
			///////////////////////
		}
		
		if(action != Animal.SLEEP)
			EyesTick();
	}
	
	
	public void paint(Graphics g){
		
	}
	
	public void paintInfo(Graphics g, int x, int y){
		super.paintInfo(g, x, y);
	}

	public static void importSmallPart(Part part, String str){
		String[] arr = BitmapFont.splitString(str, ",");
        part.m_iImgId = Integer.parseInt(arr[0]);
        part.m_iX = Integer.parseInt(arr[1]);
        part.m_iY = Integer.parseInt(arr[2]);
        part.m_iW = Integer.parseInt(arr[3]);
        part.m_iH = Integer.parseInt(arr[4]);
        part.m_nFrame = Integer.parseInt(arr[5]);
        int n = Integer.parseInt(arr[6]);
        int i;
        int index = 7;
        for(i = 0; i < n; i++){
        	part.m_arrDeltaX[i] = Byte.parseByte(arr[index++]);
        	part.m_arrDeltaY[i] = Byte.parseByte(arr[index++]);
        }
        arr = null;
	}
	
	public static void importPart(Part part, String str) {
		// TODO Auto-generated method stub
        String[] arr = BitmapFont.splitString(str, ",");
        part.m_iImgId = Integer.parseInt(arr[0]);
        part.m_iX = Integer.parseInt(arr[1]);
        part.m_iY = Integer.parseInt(arr[2]);
        part.m_iW = Integer.parseInt(arr[3]);
        part.m_iH = Integer.parseInt(arr[4]);
        part.m_nFrame = Integer.parseInt(arr[5]);
        int i;
        int index = 6;
        for(i = 0; i < 5; i++){
        	part.m_arrDeltaX[i] = Byte.parseByte(arr[index++]);
        	part.m_arrDeltaY[i] = Byte.parseByte(arr[index++]);
        }      
        arr = null;
	}

	public void importProperties(String properties) {
		// TODO Auto-generated method stub
		m_strProperties = properties;
		
		String[] arr = BitmapFont.splitString(properties, ",");
        m_iW[1] = Short.parseShort(arr[0]);
        m_iH[1] = Short.parseShort(arr[1]);
        m_iW[0] = Short.parseShort(arr[2]);
        m_iH[0] = Short.parseShort(arr[3]);
    	
        m_fSpeed[0] = 1;
        m_fSpeed[1] = Float.parseFloat(arr[4]);
        
        m_iTimeChangeFrame[0] = 2;
    	m_iTimeChangeFrame[1] = Byte.parseByte(arr[5]);
        m_iTimeChangeAction = Integer.parseInt(arr[6]);
        
        int nFrameRun = Integer.parseInt(arr[7]);
        m_arrRunFrame1 = new byte[nFrameRun];
        
        int index = 8, i;
        for(i = 0; i < nFrameRun; i++){
        	m_arrRunFrame1[i] = Byte.parseByte(arr[index + i]);
        }
        index = index + nFrameRun;
        nFrameRun = Integer.parseInt(arr[index]);
        
        m_arrRunFrame0 = new byte[nFrameRun];
        index++;
        for(i = 0; i < nFrameRun; i++){
        	m_arrRunFrame0[i] = Byte.parseByte(arr[index + i]);
        }
        
        
        arr = null;
	}
}


